package com.jarvis.adapter;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.jarvis.db.ConfigOpenHelper;
import com.jarvis.model.ConfigListItemViewHolder;
import com.jarvis.model.ConfigModel;
import com.jarvis.view.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;

public class ConfigListViewAdapter extends BaseAdapter {
	
	private boolean showCheckbox;
	
	private Set<Integer> selectedItems = new HashSet<Integer>();
	
	private LayoutInflater mInflater;
	
	private List<ConfigModel> values;
	
	private int layout;
	
	private final Context context;
	
	
	
	public ConfigListViewAdapter(Context context, List<ConfigModel> values, int layout){
		this.context = context;
		mInflater = LayoutInflater.from(context);
		this.values = values;
		this.layout = layout;
	}
	

	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ConfigListItemViewHolder holder = null;
		
		if(convertView == null){
			convertView = mInflater.inflate(layout, null);
			CheckBox checkBoxField = (CheckBox) convertView.findViewById(R.id.configlistitemcheckbox);
			TextView numberField = (TextView) convertView.findViewById(R.id.configlistitemnumber);
			TextView contentField = (TextView) convertView.findViewById(R.id.configlistitemcontent);
			TextView countField = (TextView) convertView.findViewById(R.id.configlistitemcount);
			TextView dateField = (TextView) convertView.findViewById(R.id.configlistitemdate);
			holder = new ConfigListItemViewHolder(checkBoxField, numberField, contentField, countField, dateField);
			convertView.setTag(holder);
		}else{
			holder = (ConfigListItemViewHolder) convertView.getTag();
		}
		
		holder.getPhoneNumber().setText(values.get(position).getTarget_num());
		holder.getPostContent().setText(values.get(position).getPost_content());
		holder.getCount().setText(values.get(position).getCount()+"");
		holder.getDateTime().setText(ConfigOpenHelper.DATE_FORMAT.format(new Date(values.get(position).getLast_edit())));
		
		if(showCheckbox){
			
			holder.getCheckBox().setVisibility(View.VISIBLE);
		}else{
			holder.getCheckBox().setVisibility(View.INVISIBLE);
		}
		
		if(selectedItems.contains(position)){
			holder.getCheckBox().setChecked(true);
			convertView.setSelected(true);
			convertView.setBackgroundColor(context.getResources().getColor(R.color.blue));
			convertView.setSelected(true);
		}else{
			holder.getCheckBox().setChecked(false);
			convertView.setSelected(false);
			convertView.setBackgroundColor(context.getResources().getColor(R.color.blank));
			convertView.setSelected(false);
		}
		
		return convertView;
	}

	public boolean isShowCheckbox() {
		return showCheckbox;
	}

	public void setShowCheckbox(boolean showCheckbox) {
		this.showCheckbox = showCheckbox;
	}

	public Set<Integer> getSelectedItems() {
		return selectedItems;
	}

	public void setSelectedItems(Set<Integer> selectedItems) {
		this.selectedItems = selectedItems;
	}

	public LayoutInflater getmInflater() {
		return mInflater;
	}

	public void setmInflater(LayoutInflater mInflater) {
		this.mInflater = mInflater;
	}

	public int getCount() {
		// TODO Auto-generated method stub
		if(values == null){
			return 0;
		}
		return values.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		if(values == null){
			return null;
		}
		return values.get(position);
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


	public List<ConfigModel> getValues() {
		return values;
	}


	public void setValues(List<ConfigModel> values) {
		this.values = values;
	}

	
	
}
